<template>
  <div class="app-container">
    <el-card class="box-card" shadow="hover" style="border: #ffffff">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <!--      <el-form-item label="批次ID" prop="fileId">
          <el-input
            v-model="queryParams.fileId"
            placeholder="请输入批次ID"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>-->
        <el-form-item label="批次名称" prop="fileName">
          <el-input v-model="queryParams.fileName" placeholder="请输入批次名称，冗余，⽤于查询" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="手机号" prop="tel">
          <el-input v-model="queryParams.tel" placeholder="请输入手机号" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="呼叫状态" prop="callStatus">
          <el-select v-model="queryParams.callStatus" placeholder="请选择呼叫状态" clearable size="small" >
            <el-option label="未呼叫" value="0"></el-option>
            <el-option label="已呼叫" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接听状态" prop="cdrStatus" >
          <el-select v-model="queryParams.answerStatus" placeholder="请选择接听状态" clearable size="small" >
            <el-option label="未接听" value="0"></el-option>
            <el-option label="已接听" value="1"></el-option>
          </el-select>
        </el-form-item>
        <div class="header_btn">
          <el-button size="small" type="primary" @click="handleQuery">查询</el-button>
          <el-button size="small" @click="resetQuery">重置</el-button>
        </div>
      </el-form>
      <el-card class="box-card" shadow="hover">
        <el-row :gutter="10" class="mb8">
          <right-toolbar style="float: right" :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          <el-col :span="1.5" style="float: right;margin-right: 10px">
            <el-button type="warning" plain size="small" @click="handleExport" v-hasPermi="['crm:fileTel:export']">导出</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['crm:fileTel:edit']"
            >修改</el-button>
          </el-col> -->
          <el-col :span="1.5" style="float: right">
            <el-button type="info" plain size="small" @click="handleImport" v-hasPermi="['crm:fileTel:export']">导入</el-button>
          </el-col>
          <el-col :span="1.5" style="float: right">
            <el-button type="danger" plain size="small" :disabled="multiple" @click="handleDelete" v-hasPermi="['crm:fileTel:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5" style="float: right">
            <el-button type="primary" plain size="small" @click="handleAdd" v-hasPermi="['crm:fileTel:add']">新增</el-button>
          </el-col>
        </el-row>

        <el-table border v-loading="loading" :data="fileTelList" @selection-change="handleSelectionChange" highlight-current-row>
          <el-table-column type="selection" width="55" align="center" />
          <!--      <el-table-column label="ID" align="center" prop="id" />-->
          <el-table-column label="批次ID" align="center" prop="fileId" />
          <el-table-column label="批次名称" align="center" prop="fileName" />
          <el-table-column label="手机号" align="center" prop="mobileHidden" />
          <el-table-column label="ivrId" align="center" prop="ivrId" />
          <el-table-column label="呼叫状态" align="center" prop="callStatus">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.callStatus == 1">已呼叫</el-tag>
              <el-tag type="info" v-else>未呼叫</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="接听状态" :formatter="cdrStatusFormat" prop="answerStatus">
            <template slot-scope="scope">
              <el-tag :type="scope.row.answerStatus == 22 ? 'success' : 'danger'">{{ cdrStatusFormat(scope.row) }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="描述" align="center" prop="description" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <!-- <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['crm:fileTel:edit']"
              >修改</el-button> -->
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                         v-hasPermi="['crm:fileTel:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                    @pagination="getList" />

      </el-card>
    </el-card>


    <!-- 添加或修改语音通知批次号码对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="560px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px" class="modal_form">
        <!--        <el-form-item label="批次名称" prop="fileName">
          <el-input v-model="form.fileName" placeholder="请输入批次名称，冗余，⽤于查询" />
        </el-form-item>-->
        <!--        <el-form-item label="批次ID" prop="fileId">
          <el-input v-model="form.fileId" placeholder="请输入批次ID" />
        </el-form-item>-->
        <el-form-item label="手机号" prop="tel" class="modal_form_item">
          <el-input v-model="form.tel" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="ivrId" prop="ivrId" class="modal_form_item">
          <el-input v-model="form.ivrId" placeholder="请输入ivrId" />
        </el-form-item>
        <el-form-item label="描述" prop="description" class="modal_form_item">
          <el-input type="textarea" v-model="form.description" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url"
        :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess"
        :auto-upload="false" :data="upload.data" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
            @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFileTel, getFileTel, delFileTel, addFileTel, updateFileTel } from "@/api/call/callNoticeTel";
import { getToken } from "@/utils/auth";
export default {
  name: "CallNoticeTel",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 语音通知批次号码表格数据
      fileTelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fileId: this.$route.query.id,
        fileName: null,
        tel: null,
        callStatus: null,
        answerStatus: null,
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "导入号码",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/crm/fileTel/importData",
        data: {
          fileId: this.$route.query.id || 0
        }
      },
      // 表单参数
      form: {
        fileId: this.$route.query.id || 0
      },
      // 表单校验
      rules: {
        tel: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              let isTel = /^1[3-9][0-9]{9}$/.test(value);
              if (!isTel) {
                callback(new Error('请输入正确的手机号码!'));
              } else {
                callback();
              }
            }, trigger: "blur"
          },
        ],
        fileId: [
          { required: true, message: "批次ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  dicts: ["crm_call_status"],
  activated() {
    if (this.$route.query.id != this.queryParams.fileId) {
      this.getList()
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 下载模板操作 */
    importTemplate() {
      this.download('/crm/fileTel/importTemplate', {}, `tel_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 查询语音通知批次号码列表 */
    getList() {
      this.loading = true;
      this.queryParams.fileId = this.$route.query.id || 0;
      listFileTel(this.queryParams).then(response => {
        this.fileTelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        //id: null,
        fileName: null,
        fileId: this.$route.query.id || 0,
        tel: null,
        //ivrId: null,
        //callStatus: 0,
        description: null,
        //createTime: null,
        //updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加语音通知批次号码";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFileTel(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改语音通知批次号码";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFileTel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFileTel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 呼叫状态
    cdrStatusFormat(row, column) {
      return this.selectDictLabel(this.dict.type.crm_call_status, row.answerStatus) || "-";
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除语音通知批次号码编号为"' + ids + '"的数据项？').then(function () {
        return delFileTel(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('crm/fileTel/export', {
        ...this.queryParams
      }, `fileTel_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "号码导入";
      this.upload.open = true;
    },
  }
};
</script>
<style lang="scss" scoped>
.app-container{
  //background: #f5f7f9;
  ::v-deep .el-card__header{
    padding: 0;
    line-height: 60px;
  }
  .header_btn{
    margin-top: 2px;
    display: inline-block;
    //float: right;
  }
}
.modal_form {
  .modal_form_item{
    width: 420px;
    display: inline-block
  }
  .modal_form_items{
    width: 470px;
  }
  ::v-deep .el-input__inner {
    font-size: 13px;
  }
  ::v-deep .el-textarea__inner {
    height: 120px;
  }
}
@media screen and (max-width:1480px) {
  ::v-deep .el-dialog{
    left: 130px;
  }
}
</style>
